#!/usr/bin/python
# -*- coding: UTF-8 -*-
from optparse import OptionParser
import os
import shutil
import gitBranchsUtils
WEB_MOBILE_PATH = "build/web-mobile"
BUILD_JSBDEFAULT_PATH = "build/jsb-link"


def delDirectory(folderPath):
    realPath = os.path.abspath(os.path.join(os.getcwd(), folderPath))
    if os.path.exists(realPath):
        shutil.rmtree(realPath)


def deleteBuildPath():
    delDirectory(WEB_MOBILE_PATH)
    delDirectory(BUILD_JSBDEFAULT_PATH)
    print('delete old build projects')


def autoMakeGitBranchs(workSpace):
    # 初始化git库
    gitBranchsUtils.initRepos(workSpace)
    # 切换分支
    gitBranchsUtils.autoChangeBranchs()
    gitBranchsUtils.autoChangeBranchs()
    # 拉取分支
    gitBranchsUtils.autoPullBranchs()
    gitBranchsUtils.autoPullBranchs()
    # 提交分支与子模块引用
    gitBranchsUtils.autoCommitBranchs()
    # 自动推送所有提交到远程
    gitBranchsUtils.autoPushBranchs()
    print('----autoChangeGitBranchs----')

def deleteBack():
    Branchname = os.environ['CustomerBranch']
    print("Branchname",Branchname)
    allBranchdir  = "./hotupdata_"+Branchname
    print("newversion.txt is ",os.path.abspath(os.path.join(os.getcwd(), "./newversion.txt")))
    if os.path.exists(os.path.abspath(os.path.join(os.getcwd(), "./newversion.txt"))):
        print("删除newversion版本")
        os.remove("./newversion.txt")
    print("hotupdata_install is ",os.path.abspath(os.path.join(os.getcwd(), "./hotupdata_install")))
    if os.path.exists(os.path.abspath(os.path.join(os.getcwd(), "./hotupdata_install"))):    
        print("删除hotupdata_install文件夹")
        shutil.rmtree("./hotupdata_install")
    print("hotupdata is ",os.path.abspath(os.path.join(os.getcwd(), "./hotupdata")))    
    if os.path.exists(os.path.abspath(os.path.join(os.getcwd(), "./hotupdata"))):      
        print("删除hotupdata文件夹")
        shutil.rmtree("./hotupdata")
    print("allBranchdir",allBranchdir)    
    print("allBranchdir is ",os.path.abspath(os.path.join(os.getcwd(), allBranchdir))) 
    if os.path.exists(os.path.abspath(os.path.join(os.getcwd(), allBranchdir))):      
        print("移动备份文件夹：",allBranchdir,"到hotupdata")
        shutil.move(allBranchdir, "./hotupdata")

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option('-t', '--buildType', dest="buildType")
    (option, args) = parser.parse_args()
